package com.opengms.HydroModelsIntegration.Dao;

import com.opengms.HydroModelsIntegration.Domain.InpData;
import com.opengms.HydroModelsIntegration.Domain.InpSection;
import net.sf.json.JSONObject;
import org.apache.commons.beanutils.ConvertUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class InpParser implements IInpParser {


    private InpData inpData=new InpData();  //TODO 待优化，解耦


    public InpData parseInp(String fileName)
    {

        BufferedReader bufferedReader=openFile(fileName);
        BufferedReader bufferedReaderNextline=openFile(fileName);
        String line=null;
        String nextline=null;
        try {
            nextline=bufferedReaderNextline.readLine();
            while ((line=bufferedReader.readLine())!=null)
            {
                switch (line) {

                    case "":break;
                    case "[TITLE]":
                        inpData.setTitleArrayList(readTitle(bufferedReader, bufferedReaderNextline));break;
                    case "[OPTIONS]":
                        inpData.setOption(readOption(bufferedReader, bufferedReaderNextline));break;
                    case "[EVAPORATION]":
                        inpData.setEvaporation(readEvaporation(bufferedReader, bufferedReaderNextline));break;
                    case "[RAINGAGES]":
                        inpData.setRaingageArrayList(readRaingage(bufferedReader, bufferedReaderNextline));break;
                    case "[SUBCATCHMENTS]":
                        inpData.setSubcatchmentArrayList(readSubcatchment(bufferedReader, bufferedReaderNextline));break;
                    case "[SUBAREAS]":
                        inpData.setSubareaArrayList(readSubarea(bufferedReader, bufferedReaderNextline));break;
                    case "[INFILTRATION]":
                        inpData.setInfiltrationArrayList(readInfiltration(bufferedReader, bufferedReaderNextline));break;
                    case "[JUNCTIONS]":
                        inpData.setJunctionArrayList(readJunction(bufferedReader, bufferedReaderNextline));break;
                    case "[OUTFALLS]":
                        inpData.setOutfallArrayList(readOutfall(bufferedReader, bufferedReaderNextline));break;
                    case "[STORAGE]":
                        inpData.setStorageArrayList(readStorage(bufferedReader, bufferedReaderNextline));break;
                    case "[CONDUITS]":
                        inpData.setConduitArrayList(readConduit(bufferedReader, bufferedReaderNextline));break;
                    case "[XSECTIONS]":
                        inpData.setXscetionArrayList(readXscetion(bufferedReader, bufferedReaderNextline));break;
                    case "[LOSSES]":
                        inpData.setLossesArrayList(readLosses(bufferedReader, bufferedReaderNextline));break;
                    case "[CURVES]":
                        inpData.setCurveArrayList(readCurve(bufferedReader, bufferedReaderNextline));break;
                    case "[TIMESERIES]":
                        inpData.setTimeseriesArrayList(readTimeseries(bufferedReader, bufferedReaderNextline));break;
                    case "[REPORT]":
                        inpData.setReport(readReport(bufferedReader, bufferedReaderNextline));break;
                    case "[TAGS]":
                        inpData.setTagArrayList(readTag(bufferedReader, bufferedReaderNextline));break;
                    case "[MAP]":
                        inpData.setMap(readMap(bufferedReader, bufferedReaderNextline));break;
                    case "[COORDINATES]":
                        inpData.setCoordinateArrayList(readCoordinate(bufferedReader, bufferedReaderNextline));break;
                    case "[VERTICES]":
                        inpData.setVerticeArrayList(readVertice(bufferedReader, bufferedReaderNextline));break;
                    case "[Polygons]":
                        inpData.setPolygonArrayList(readPolygons(bufferedReader, bufferedReaderNextline));break;
                    case "[SYMBOLS]":
                        inpData.setSymbolArrayList(readSymbol(bufferedReader, bufferedReaderNextline));break;
                    default:
                        readBlankLine(bufferedReader, bufferedReaderNextline);break;
                    }
                }
            return inpData;
        } catch (IOException e) {

        }
        return null;
    }

    /*打开文件，并得到可以按行读取文件的bufferedReader*/
    @Override
    public BufferedReader openFile(String fileName)
    {
        File file=new File(fileName);
        BufferedReader bufferedReader=null;
        if (file.isFile() && file.exists())
        {
            try {
                //使用字符流读取文件
                FileReader reader=new FileReader(file);
                //将普通的Reader包装成BufferedReader,就可以使用BufferedReader中的readline方法
                bufferedReader=new BufferedReader(reader);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
        return bufferedReader;
    }

    public void readBlankLine(BufferedReader bufferedReader,BufferedReader bufferedReaderNextline)
    {
        String nextline=null;
        String line= null;
        try {
            nextline=bufferedReaderNextline.readLine();

            while ((line=bufferedReader.readLine())!=null && (nextline=bufferedReaderNextline.readLine())!=null)
            {
                if (nextSection(nextline))
                {
                    break;
                }
            }
            return;

        } catch (IOException e) {
            e.printStackTrace();
        }
        return;
    }

    /*读取[TITLE]*/
    @Override
    public ArrayList<String> readTitle(BufferedReader bufferedReader,BufferedReader bufferedReaderNextline)
    {
        ArrayList<String> titleArrayList=new ArrayList<String>();
        String nextline=null;
        String line= null;

        try {
            nextline=bufferedReaderNextline.readLine();
            while ((line=bufferedReader.readLine())!=null && (nextline=bufferedReaderNextline.readLine())!=null)
            {
                if (line.startsWith(";;"))
                    continue;

                if (nextSection(nextline))
                {
                    if (!line.equals(""))
                    {
                        titleArrayList.add(line);
                    }
                    break;
                }
                if (line.equals(""))
                    continue;
                titleArrayList.add(line);
            }
            return titleArrayList;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /*读取[OPTIONS]*/
    @Override
    public InpData.Option readOption(BufferedReader bufferedReader,BufferedReader bufferedReaderNextline)
    {
        String line=null;
        String nextline=null;
        InpData.Option option=inpData.new Option();
        try
        {
            option.setIgnore_rainfall("NO");
            option.setIgnore_routing("NO");
            nextline=bufferedReaderNextline.readLine();
            while ((line=bufferedReader.readLine())!=null &&  (nextline=bufferedReaderNextline.readLine())!=null)
            {
                String[] tempStr=line.split("[ ]+");
                if (line.startsWith(";;"))
                    continue;
                if (nextSection(nextline))
                {
                    if (!line.equals(""))
                    {
                        if (tempStr[0].equals("FLOW_UNITS"))
                            option.setFlow_units(tempStr[1]);
                        else if (tempStr[0].equals("INFILTRATION"))
                            option.setInfiltration(tempStr[1]);
                        else if (tempStr[0].equals("FLOW_ROUTING"))
                            option.setFlow_routing(tempStr[1]);
                        else if (tempStr[0].equals("LINK_OFFSETS"))
                            option.setLink_offsets(tempStr[1]);
                        else if (tempStr[0].equals("MIN_SLOPE"))
                            option.setMin_slope(Float.parseFloat(tempStr[1]));
                        else if (tempStr[0].equals("ALLOW_PONDING"))
                            option.setAllow_ponding(tempStr[1]);
                        else if (tempStr[0].equals("SKIP_STEADY_STATE"))
                            option.setSkip_steady_state(tempStr[1]);
                        else if (tempStr[0].equals("IGNORE_RAINFALL"))
                            option.setIgnore_rainfall(tempStr[1]);
                        else if (tempStr[0].equals("IGNORE_ROUTING"))
                            option.setIgnore_routing(tempStr[1]);
                        else if (tempStr[0].equals("START_DATE"))
                            option.setStart_date(tempStr[1]);
                        else if (tempStr[0].equals("START_TIME"))
                            option.setStart_time(tempStr[1]);
                        else if (tempStr[0].equals("REPORT_START_DATE"))
                            option.setReport_start_date(tempStr[1]);
                        else if (tempStr[0].equals("REPORT_START_TIME"))
                            option.setReport_start_time(tempStr[1]);
                        else if (tempStr[0].equals("END_DATE"))
                            option.setEnd_date(tempStr[1]);
                        else if (tempStr[0].equals("END_TIME"))
                            option.setEnd_time(tempStr[1]);
                        else if (tempStr[0].equals("SWEEP_START"))
                            option.setSweep_start(tempStr[1]);
                        else if (tempStr[0].equals("SWEEP_END"))
                            option.setSweep_end(tempStr[1]);
                        else if (tempStr[0].equals("DRY_DAYS"))
                            option.setDry_days(Float.parseFloat(tempStr[1]));
                        else if (tempStr[0].equals("REPORT_STEP"))
                            option.setReport_step(tempStr[1]);
                        else if (tempStr[0].equals("WET_STEP"))
                            option.setWet_step(tempStr[1]);
                        else if (tempStr[0].equals("DRY_STEP"))
                            option.setDry_step(tempStr[1]);
                        else if (tempStr[0].equals("ROUTING_STEP"))
                            option.setRouting_step(tempStr[1]);
                        else if (tempStr[0].equals("INERTIAL_DAMPING"))
                            option.setInertial_damping(tempStr[1]);
                        else if (tempStr[0].equals("NORMAL_FLOW_LIMITED"))
                            option.setNormal_flow_limited(tempStr[1]);
                        else if (tempStr[0].equals("FORCE_MAIN_EQUATION"))
                            option.setForce_main_equation(tempStr[1]);
                        else if (tempStr[0].equals("VARIABLE_STEP"))
                            option.setVariable_step(Float.parseFloat(tempStr[1]));
                        else if (tempStr[0].equals("LENGTHENING_STEP"))
                            option.setLengthening_step(Float.parseFloat(tempStr[1]));
                        else if (tempStr[0].equals("MIN_SURFAREA"))
                            option.setMin_surfarea(Float.parseFloat(tempStr[1]));
                        else if (tempStr[0].equals("MAX_TRIALS"))
                            option.setMax_trials(Float.parseFloat(tempStr[1]));
                        else if (tempStr[0].equals("HEAD_TOLERANCE"))
                            option.setHead_tolerance(Float.parseFloat(tempStr[1]));
                        else if (tempStr[0].equals("SYS_FLOW_TOL"))
                            option.setSys_flow_tol(Float.parseFloat(tempStr[1]));
                        else if (tempStr[0].equals("LAT_FLOW_TOL"))
                            option.setLat_flow_tol(Float.parseFloat(tempStr[1]));
                        else
                        {     }
                    }
                    break;
                }
                if (line.equals(""))
                    continue;

                if (tempStr[0].equals("FLOW_UNITS"))
                    option.setFlow_units(tempStr[1]);
                else if (tempStr[0].equals("INFILTRATION"))
                    option.setInfiltration(tempStr[1]);
                else if (tempStr[0].equals("FLOW_ROUTING"))
                    option.setFlow_routing(tempStr[1]);
                else if (tempStr[0].equals("LINK_OFFSETS"))
                    option.setLink_offsets(tempStr[1]);
                else if (tempStr[0].equals("MIN_SLOPE"))
                    option.setMin_slope(Float.parseFloat(tempStr[1]));
                else if (tempStr[0].equals("ALLOW_PONDING"))
                    option.setAllow_ponding(tempStr[1]);
                else if (tempStr[0].equals("SKIP_STEADY_STATE"))
                    option.setSkip_steady_state(tempStr[1]);
                else if (tempStr[0].equals("IGNORE_RAINFALL"))
                    option.setIgnore_rainfall(tempStr[1]);
                else if (tempStr[0].equals("IGNORE_ROUTING"))
                    option.setIgnore_routing(tempStr[1]);
                else if (tempStr[0].equals("START_DATE"))
                    option.setStart_date(tempStr[1]);
                else if (tempStr[0].equals("START_TIME"))
                    option.setStart_time(tempStr[1]);
                else if (tempStr[0].equals("REPORT_START_DATE"))
                    option.setReport_start_date(tempStr[1]);
                else if (tempStr[0].equals("REPORT_START_TIME"))
                    option.setReport_start_time(tempStr[1]);
                else if (tempStr[0].equals("END_DATE"))
                    option.setEnd_date(tempStr[1]);
                else if (tempStr[0].equals("END_TIME"))
                    option.setEnd_time(tempStr[1]);
                else if (tempStr[0].equals("SWEEP_START"))
                    option.setSweep_start(tempStr[1]);
                else if (tempStr[0].equals("SWEEP_END"))
                    option.setSweep_end(tempStr[1]);
                else if (tempStr[0].equals("DRY_DAYS"))
                    option.setDry_days(Float.parseFloat(tempStr[1]));
                else if (tempStr[0].equals("REPORT_STEP"))
                    option.setReport_step(tempStr[1]);
                else if (tempStr[0].equals("WET_STEP"))
                    option.setWet_step(tempStr[1]);
                else if (tempStr[0].equals("DRY_STEP"))
                    option.setDry_step(tempStr[1]);
                else if (tempStr[0].equals("ROUTING_STEP"))
                    option.setRouting_step(tempStr[1]);
                else if (tempStr[0].equals("INERTIAL_DAMPING"))
                    option.setInertial_damping(tempStr[1]);
                else if (tempStr[0].equals("NORMAL_FLOW_LIMITED"))
                    option.setNormal_flow_limited(tempStr[1]);
                else if (tempStr[0].equals("FORCE_MAIN_EQUATION"))
                    option.setForce_main_equation(tempStr[1]);
                else if (tempStr[0].equals("VARIABLE_STEP"))
                    option.setVariable_step(Float.parseFloat(tempStr[1]));
                else if (tempStr[0].equals("LENGTHENING_STEP"))
                    option.setLengthening_step(Float.parseFloat(tempStr[1]));
                else if (tempStr[0].equals("MIN_SURFAREA"))
                    option.setMin_surfarea(Float.parseFloat(tempStr[1]));
                else if (tempStr[0].equals("MAX_TRIALS"))
                    option.setMax_trials(Float.parseFloat(tempStr[1]));
                else if (tempStr[0].equals("HEAD_TOLERANCE"))
                    option.setHead_tolerance(Float.parseFloat(tempStr[1]));
                else if (tempStr[0].equals("SYS_FLOW_TOL"))
                    option.setSys_flow_tol(Float.parseFloat(tempStr[1]));
                else if (tempStr[0].equals("LAT_FLOW_TOL"))
                    option.setLat_flow_tol(Float.parseFloat(tempStr[1]));
                else
                {     }
            }
            return option;

        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    /*读取[EVAPORATION]*/
    @Override
    public InpData.Evaporation readEvaporation(BufferedReader bufferedReader,BufferedReader bufferedReaderNextline)
    {
        String line=null;
        String nextline=null;
        InpData.Evaporation evaporation=inpData.new Evaporation();
        try {
            nextline=bufferedReaderNextline.readLine();
            while ((line=bufferedReader.readLine())!=null && (nextline=bufferedReaderNextline.readLine())!=null )
            {
                String[] tempStr=line.split("[ ]+");
                if (line.startsWith(";;"))
                    continue;

                if (nextSection(nextline))
                {
                    if (!line.equals(""))
                    {
                        if (tempStr[0].equals("CONSTANT"))
                        {
                            evaporation.setConstant(Float.parseFloat(tempStr[1]));
                            evaporation.setSelected_source("CONSTANT");
                        }
                        else if (tempStr[0].equals("TIMESERIES"))
                        {
                            evaporation.setTimeseries(tempStr[1]);
                            evaporation.setSelected_source("TIMESERIES");
                        }
                        else if (tempStr[0].equals("FILE"))
                        {
                            evaporation.setFile((float[]) ConvertUtils.convert(tempStr,float.class));
                            evaporation.setSelected_source("FILE");
                        }
                        else if (tempStr[0].equals("MONTHLY"))
                        {
                            evaporation.setMonthly((float[]) ConvertUtils.convert(tempStr,float.class));
                            evaporation.setSelected_source("MONTHLY");
                        }
                        else if (tempStr[0].equals("TEMPERATURE"))
                        {
                            evaporation.setTemperature(tempStr[1]);
                            evaporation.setSelected_source("TEMPERATURE");
                        }
                        else if (tempStr[0].equals("RECOVERY"))
                        {
                            evaporation.setRecovery(tempStr[1]);
                        }
                        else if (tempStr[0].equals("DRY_ONLY"))
                        {
                            evaporation.setDryonly(tempStr[1]);
                        }
                        else
                        {

                        }
                    }
                    break;
                }
                if (line.equals(""))
                    continue;

                if (tempStr[0].equals("CONSTANT"))
                {
                    evaporation.setConstant(Float.parseFloat(tempStr[1]));
                    evaporation.setSelected_source("CONSTANT");
                }
                else if (tempStr[0].equals("TIMESERIES"))
                {
                    evaporation.setTimeseries(tempStr[1]);
                    evaporation.setSelected_source("TIMESERIES");
                }
                else if (tempStr[0].equals("FILE"))
                {
                    evaporation.setFile((float[]) ConvertUtils.convert(tempStr,float.class));
                    evaporation.setSelected_source("FILE");
                }
                else if (tempStr[0].equals("MONTHLY"))
                {
                    evaporation.setMonthly((float[]) ConvertUtils.convert(tempStr,float.class));
                    evaporation.setSelected_source("MONTHLY");
                }
                else if (tempStr[0].equals("TEMPERATURE"))
                {
                    evaporation.setTemperature(tempStr[1]);
                    evaporation.setSelected_source("TEMPERATURE");
                }
                else if (tempStr[0].equals("RECOVERY"))
                {
                    evaporation.setRecovery(tempStr[1]);
                }
                else if (tempStr[0].equals("DRY_ONLY"))
                {
                    evaporation.setDryonly(tempStr[1]);
                }
                else
                {

                }

            }
            return evaporation;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    /*读取[RAINGAGES]*/
    @Override
    public ArrayList<InpData.Raingage> readRaingage(BufferedReader bufferedReader,BufferedReader bufferedReaderNextline)
    {
        String nextline=null;
        String line= null;
        ArrayList<InpData.Raingage> raingageArrayList=new ArrayList<>();
        try {
            nextline=bufferedReaderNextline.readLine();

            while ((line=bufferedReader.readLine())!=null && (nextline=bufferedReaderNextline.readLine())!=null)
            {
                InpData.Raingage raingage=inpData.new Raingage();
//                String[] tempDescription
                if (line.startsWith(";;"))
                    continue;
                else if(line.startsWith(";"))
                {
                    //TODO
                    ArrayList<String> tempDescription= new ArrayList<>();

                    while (line.startsWith(";"))
                    {
                        tempDescription.add(line.substring(1));
                        line=bufferedReader.readLine();
                        nextline=bufferedReaderNextline.readLine();
                    }

                    raingage.setDescription(tempDescription);

                }

                String[] tempStr=line.split("[ ]+");
                if (nextSection(nextline))
                {
                    if (!line.equals(""))
                    {
                        raingage.setGage(tempStr[0]);
                        raingage.setFormat(tempStr[1]);
                        raingage.setInterval(tempStr[2]);
                        raingage.setScf(tempStr[3]);
                        raingage.setDatasource(tempStr[4]);
                        if (raingage.getDatasource().equals("TIMESERIES"))
                        {
                            raingage.setSeriesname(tempStr[5]);
                        }
                        else if (raingage.getDatasource().equals("FILE"))
                        {
                            raingage.setFilename(tempStr[5]);
                            raingage.setStationid(tempStr[6]);
                            raingage.setRainunits(tempStr[7]);
                        }
                        else
                        {

                        }
                        raingageArrayList.add(raingage);
                    }
                    break;
                }
                if (line.equals(""))
                    continue;
                raingage.setGage(tempStr[0]);
                raingage.setFormat(tempStr[1]);
                raingage.setInterval(tempStr[2]);
                raingage.setScf(tempStr[3]);
                raingage.setDatasource(tempStr[4]);
                if (raingage.getDatasource().equals("TIMESERIES"))
                {
                    raingage.setSeriesname(tempStr[5]);
                }
                else if (raingage.getDatasource().equals("FILE"))
                {
                    raingage.setFilename(tempStr[5]);
                    raingage.setStationid(tempStr[6]);
                    raingage.setRainunits(tempStr[7]);
                }
                else
                {

                }
                raingageArrayList.add(raingage);
            }
            return raingageArrayList;

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /*读取[SUBCATCHMENTS]*/
    @Override
    public ArrayList<InpData.Subcatchment> readSubcatchment(BufferedReader bufferedReader,BufferedReader bufferedReaderNextline)
    {
        String nextline=null;
        String line= null;
        ArrayList<InpData.Subcatchment> subcatchmentArrayList=new ArrayList<>();
        try {
            nextline=bufferedReaderNextline.readLine();

            while ((line=bufferedReader.readLine())!=null && (nextline=bufferedReaderNextline.readLine())!=null)
            {
                InpData.Subcatchment subcatchment=inpData.new Subcatchment();
                if (line.startsWith(";;"))
                    continue;
                else if(line.startsWith(";"))
                {

                    ArrayList<String> tempDescription= new ArrayList<>();

                    while (line.startsWith(";"))
                    {
                        tempDescription.add(line.substring(1));
                        line=bufferedReader.readLine();
                        nextline=bufferedReaderNextline.readLine();
                    }
                    subcatchment.setDescription(tempDescription);

                }
                String[] tempStr=line.split("[ ]+");
                if (nextSection(nextline))
                {
                    if (!line.equals(""))
                    {
                        subcatchment.setSubcatchment(tempStr[0]);
                        subcatchment.setRaingage(tempStr[1]);
                        subcatchment.setOutlet(tempStr[2]);
                        subcatchment.setArea(Float.parseFloat(tempStr[3]));
                        subcatchment.setImperv(Float.parseFloat(tempStr[4]));
                        subcatchment.setWidth(Float.parseFloat(tempStr[5]));
                        subcatchment.setSlope(Float.parseFloat(tempStr[6]));
                        subcatchment.setCurblen(Float.parseFloat(tempStr[7]));

                        if (tempStr.length>8 && !tempStr[8].equals(""))
                        {
                            subcatchment.setSnowpack(tempStr[8]);
                        }
                        else
                        {
                            subcatchment.setSnowpack("");
                        }

                        subcatchmentArrayList.add(subcatchment);
                    }
                    break;
                }
                if (line.equals(""))
                    continue;
                subcatchment.setSubcatchment(tempStr[0]);
                subcatchment.setRaingage(tempStr[1]);
                subcatchment.setOutlet(tempStr[2]);
                subcatchment.setArea(Float.parseFloat(tempStr[3]));
                subcatchment.setImperv(Float.parseFloat(tempStr[4]));
                subcatchment.setWidth(Float.parseFloat(tempStr[5]));
                subcatchment.setSlope(Float.parseFloat(tempStr[6]));
                subcatchment.setCurblen(Float.parseFloat(tempStr[7]));

                if (tempStr.length>8 && !tempStr[8].equals(""))
                {
                    subcatchment.setSnowpack(tempStr[8]);
                }
                else
                {
                    subcatchment.setSnowpack("");
                }

                subcatchmentArrayList.add(subcatchment);
            }
            return subcatchmentArrayList;

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /*读取[SUBAREAS]*/
    @Override
    public ArrayList<InpData.Subarea> readSubarea(BufferedReader bufferedReader,BufferedReader bufferedReaderNextline)
    {
        String nextline=null;
        String line= null;
        ArrayList<InpData.Subarea> subareaArrayList=new ArrayList<>();
        try {
            nextline=bufferedReaderNextline.readLine();

            while ((line=bufferedReader.readLine())!=null && (nextline=bufferedReaderNextline.readLine())!=null)
            {
                InpData.Subarea subarea=inpData.new Subarea();
                if (line.startsWith(";;"))
                    continue;
                String[] tempStr=line.split("[ ]+");
                if (nextSection(nextline))
                {
                    if (!line.equals(""))
                    {
                        subarea.setSubcatchment(tempStr[0]);
                        subarea.setN_imperv(Float.parseFloat(tempStr[1]));
                        subarea.setN_perv(Float.parseFloat(tempStr[2]));
                        subarea.setS_imperv(Float.parseFloat(tempStr[3]));
                        subarea.setS_perv(Float.parseFloat(tempStr[4]));
                        subarea.setPctzero(Float.parseFloat(tempStr[5]));
                        subarea.setRouteto(tempStr[6]);
                        if (tempStr.length>7 && !tempStr[7].equals(""))
                        {
                            subarea.setPctrouted(Float.parseFloat(tempStr[7]));
                        }
                        else
                        {
                            subarea.setPctrouted(100);
                        }

                        subareaArrayList.add(subarea);
                    }
                    break;
                }
                if (line.equals(""))
                    continue;
                subarea.setSubcatchment(tempStr[0]);
                subarea.setN_imperv(Float.parseFloat(tempStr[1]));
                subarea.setN_perv(Float.parseFloat(tempStr[2]));
                subarea.setS_imperv(Float.parseFloat(tempStr[3]));
                subarea.setS_perv(Float.parseFloat(tempStr[4]));
                subarea.setPctzero(Float.parseFloat(tempStr[5]));
                subarea.setRouteto(tempStr[6]);
                if (tempStr.length>7 && !tempStr[7].equals(""))
                {
                    subarea.setPctrouted(Float.parseFloat(tempStr[7]));
                }
                else
                {
                    subarea.setPctrouted(100);
                }

                subareaArrayList.add(subarea);
            }
            return subareaArrayList;

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /*读取[INFILTRATION]*/
    @Override
    public ArrayList<InpData.Infiltration> readInfiltration(BufferedReader bufferedReader,BufferedReader bufferedReaderNextline)
    {
        String nextline=null;
        String line= null;
        ArrayList<InpData.Infiltration> infiltrationArrayList=new ArrayList<>();
        try {
            nextline=bufferedReaderNextline.readLine();
            switch (inpData.getOption().getInfiltration())
            {
                case "HORTON":
                case "MODIFIED_HORTON":
                    while ((line=bufferedReader.readLine())!=null && (nextline=bufferedReaderNextline.readLine())!=null)
                    {
                        InpData.Infiltration infiltration=inpData.new Infiltration();
                        if (line.startsWith(";;"))
                            continue;
                        String[] tempStr=line.split("[ ]+");
                        if (nextSection(nextline))
                        {
                            if (!line.equals(""))
                            {
                                infiltration.setSubcatchment(tempStr[0]);
                                infiltration.setMaxrate(Float.parseFloat(tempStr[1]));
                                infiltration.setMinrate(Float.parseFloat(tempStr[2]));
                                infiltration.setDecay(Float.parseFloat(tempStr[3]));
                                infiltration.setDrytime(Float.parseFloat(tempStr[4]));
                                infiltration.setMaxinfil(Float.parseFloat(tempStr[5]));
                                infiltrationArrayList.add(infiltration);
                            }
                            break;
                        }
                        if (line.equals(""))
                            continue;
                        infiltration.setSubcatchment(tempStr[0]);
                        infiltration.setMaxrate(Float.parseFloat(tempStr[1]));
                        infiltration.setMinrate(Float.parseFloat(tempStr[2]));
                        infiltration.setDecay(Float.parseFloat(tempStr[3]));
                        infiltration.setDrytime(Float.parseFloat(tempStr[4]));
                        infiltration.setMaxinfil(Float.parseFloat(tempStr[5]));
                        infiltrationArrayList.add(infiltration);
                    }
                    break;
                case "GREEN_AMPT":
                    while ((line=bufferedReader.readLine())!=null && (nextline=bufferedReaderNextline.readLine())!=null)
                    {
                        InpData.Infiltration infiltration=inpData.new Infiltration();
                        if (line.startsWith(";;"))
                            continue;
                        String[] tempStr=line.split("[ ]+");
                        if (nextSection(nextline))
                        {
                            if (!line.equals(""))
                            {
                                infiltration.setSubcatchment(tempStr[0]);
                                infiltration.setSuction(Float.parseFloat(tempStr[1]));
                                infiltration.setConductivity(Float.parseFloat(tempStr[2]));
                                infiltration.setDrytime(Float.parseFloat(tempStr[3]));

                                infiltrationArrayList.add(infiltration);
                            }
                            break;
                        }
                        if (line.equals(""))
                            continue;
                        infiltration.setSubcatchment(tempStr[0]);
                        infiltration.setSuction(Float.parseFloat(tempStr[1]));
                        infiltration.setConductivity(Float.parseFloat(tempStr[2]));
                        infiltration.setDrytime(Float.parseFloat(tempStr[3]));
                        infiltrationArrayList.add(infiltration);
                    }
                    break;
                case "CURVE_NUMBER":
                    while ((line=bufferedReader.readLine())!=null && (nextline=bufferedReaderNextline.readLine())!=null)
                    {
                        InpData.Infiltration infiltration=inpData.new Infiltration();
                        if (line.startsWith(";;"))
                            continue;
                        String[] tempStr=line.split("[ ]+");
                        if (nextSection(nextline))
                        {
                            if (!line.equals(""))
                            {
                                infiltration.setSubcatchment(tempStr[0]);
                                infiltration.setCurveNumber(Float.parseFloat(tempStr[1]));
                                infiltration.setConductivity(Float.parseFloat(tempStr[2]));
                                infiltration.setDrytime(Float.parseFloat(tempStr[3]));

                                infiltrationArrayList.add(infiltration);
                            }
                            break;
                        }
                        if (line.equals(""))
                            continue;
                        infiltration.setSubcatchment(tempStr[0]);
                        infiltration.setCurveNumber(Float.parseFloat(tempStr[1]));
                        infiltration.setConductivity(Float.parseFloat(tempStr[2]));
                        infiltration.setDrytime(Float.parseFloat(tempStr[3]));
                        infiltrationArrayList.add(infiltration);
                    }
                    break;
                default:
                    break;

            }


            return infiltrationArrayList;

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /*读取[JUNCTIONS]*/
    @Override
    public ArrayList<InpData.Junction> readJunction(BufferedReader bufferedReader,BufferedReader bufferedReaderNextline)
    {
        String nextline=null;
        String line= null;
        ArrayList<InpData.Junction> junctionArrayList=new ArrayList<>();


        try {
            nextline=bufferedReaderNextline.readLine();

            while ((line=bufferedReader.readLine())!=null && (nextline=bufferedReaderNextline.readLine())!=null)
            {
                InpData.Junction junction=inpData.new Junction();
                if (line.startsWith(";;"))
                    continue;
                else if(line.startsWith(";"))
                {
                    ArrayList<String> tempDescription= new ArrayList<>();

                    while (line.startsWith(";"))
                    {
                        tempDescription.add(line.substring(1));
                        line=bufferedReader.readLine();
                        nextline=bufferedReaderNextline.readLine();
                    }
                    junction.setDescription(tempDescription);

                }
                String[] tempStr=line.split("[ ]+");
                if (nextSection(nextline))
                {
                    if (!line.equals(""))
                    {
                        junction.setJunction(tempStr[0]);
                        junction.setInvert(Float.parseFloat(tempStr[1]));
                        junction.setMaxdepth(Float.parseFloat(tempStr[2]));
                        junction.setInitdepth(Float.parseFloat(tempStr[3]));
                        junction.setSurdepth(Float.parseFloat(tempStr[4]));
                        junction.setAponded(Float.parseFloat(tempStr[5]));
                        junctionArrayList.add(junction);
                    }
                    break;
                }
                if (line.equals(""))
                    continue;
                junction.setJunction(tempStr[0]);
                junction.setInvert(Float.parseFloat(tempStr[1]));
                junction.setMaxdepth(Float.parseFloat(tempStr[2]));
                junction.setInitdepth(Float.parseFloat(tempStr[3]));
                junction.setSurdepth(Float.parseFloat(tempStr[4]));
                junction.setAponded(Float.parseFloat(tempStr[5]));
                junctionArrayList.add(junction);
            }
            return junctionArrayList;

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /*读取[OUTFALLS]*/
    @Override
    public ArrayList<InpData.Outfall> readOutfall(BufferedReader bufferedReader,BufferedReader bufferedReaderNextline)
    {
        String nextline=null;
        String line= null;
        ArrayList<InpData.Outfall> outfallArrayList=new ArrayList<>();
        try {
            nextline=bufferedReaderNextline.readLine();

            while ((line=bufferedReader.readLine())!=null && (nextline=bufferedReaderNextline.readLine())!=null)
            {
                InpData.Outfall outfall=inpData.new Outfall();
                if (line.startsWith(";;"))
                    continue;
                else if(line.startsWith(";"))
                {
                    ArrayList<String> tempDescription= new ArrayList<>();

                    while (line.startsWith(";"))
                    {
                        tempDescription.add(line.substring(1));
                        line=bufferedReader.readLine();
                        nextline=bufferedReaderNextline.readLine();
                    }
                    outfall.setDescription(tempDescription);

                }
                String[] tempStr=line.split("[ ]+");
                if (nextSection(nextline))
                {
                    if (!line.equals(""))
                    {
                        outfall.setOutfall(tempStr[0]);
                        outfall.setInvert(Float.parseFloat(tempStr[1]));
                        outfall.setType(tempStr[2]);

                        if ((outfall.getType()).equals("FREE") || (outfall.getType()).equals("NORMAL"))
                        {
                            outfall.setStagedata("");
                            outfall.setGated(tempStr[3]);
                        }
                        if ((outfall.getType()).equals("FIXED") || (outfall.getType()).equals("TIDAL") || (outfall.getType()).equals("TIMESERIES"))
                        {
                            outfall.setStagedata(tempStr[3]);
                            outfall.setGated(tempStr[4]);
                        }
                        outfallArrayList.add(outfall);
                    }
                    break;
                }
                if (line.equals(""))
                    continue;
                outfall.setOutfall(tempStr[0]);
                outfall.setInvert(Float.parseFloat(tempStr[1]));
                outfall.setType(tempStr[2]);

                if ((outfall.getType()).equals("FREE") || (outfall.getType()).equals("NORMAL"))
                {
                    outfall.setStagedata("");
                    outfall.setGated(tempStr[3]);
                }
                if ((outfall.getType()).equals("FIXED") || (outfall.getType()).equals("TIDAL") || (outfall.getType()).equals("TIMESERIES"))
                {
                    outfall.setStagedata(tempStr[3]);
                    outfall.setGated(tempStr[4]);
                }
                outfallArrayList.add(outfall);
            }
            return outfallArrayList;

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /*读取[STORAGE]*/
    @Override
    public ArrayList<InpData.Storage> readStorage(BufferedReader bufferedReader,BufferedReader bufferedReaderNextline)
    {
        String nextline=null;
        String line= null;
        ArrayList<InpData.Storage> storageArrayList=new ArrayList<>();
        try {
            nextline=bufferedReaderNextline.readLine();

            while ((line=bufferedReader.readLine())!=null && (nextline=bufferedReaderNextline.readLine())!=null)
            {
                InpData.Storage storage=inpData.new Storage();
                if (line.startsWith(";;"))
                    continue;
                else if(line.startsWith(";"))
                {
                    ArrayList<String> tempDescription= new ArrayList<>();

                    while (line.startsWith(";"))
                    {
                        tempDescription.add(line.substring(1));
                        line=bufferedReader.readLine();
                        nextline=bufferedReaderNextline.readLine();
                    }
                    storage.setDescription(tempDescription);

                }
                String[] tempStr=line.split("[ ]+");
                if (nextSection(nextline))
                {
                    if (!line.equals(""))
                    {
                        storage.setStorage(tempStr[0]);
                        storage.setInvert(Float.parseFloat(tempStr[1]));
                        storage.setMaxdepth(Float.parseFloat(tempStr[2]));
                        storage.setInitdepth(Float.parseFloat(tempStr[3]));
                        storage.setShape(tempStr[4]);

                        if ((storage.getShape()).equals("TABULAR"))
                        {
                            storage.setCurvename(tempStr[5]);
                            storage.setPonded(Float.parseFloat(tempStr[6]));
                            storage.setFevap(Float.parseFloat(tempStr[7]));
                            if (tempStr.length>8 && !tempStr[8].equals(""))
                            {
                                float[] tempSeepage=new float[3];
                                tempSeepage[0]=(Float.parseFloat(tempStr[8]));
                                tempSeepage[1]=(Float.parseFloat(tempStr[9]));
                                tempSeepage[2]=(Float.parseFloat(tempStr[10]));
                                storage.setSeepage(tempSeepage);
                                storage.setSeepage_exist("YES");
                            }
                            else
                            {
                                storage.setSeepage_exist("NO");
                            }
                        }
                        else if ((storage.getShape()).equals("FUNCTIONAL"))
                        {
                            float[] tempParam=new float[3];
                            tempParam[0]=(Float.parseFloat(tempStr[5]));
                            tempParam[1]=(Float.parseFloat(tempStr[6]));
                            tempParam[2]=(Float.parseFloat(tempStr[7]));
                            storage.setParam(tempParam);
                            storage.setPonded(Float.parseFloat(tempStr[8]));
                            storage.setFevap(Float.parseFloat(tempStr[9]));
                            if (tempStr.length>10 && !tempStr[8].equals(""))
                            {
                                float[] tempSeepage=new float[3];
                                tempSeepage[0]=(Float.parseFloat(tempStr[10]));
                                tempSeepage[1]=(Float.parseFloat(tempStr[11]));
                                tempSeepage[2]=(Float.parseFloat(tempStr[12]));
                                storage.setSeepage(tempSeepage);
                                storage.setSeepage_exist("YES");
                            }
                            else
                            {
                                storage.setSeepage_exist("NO");
                            }
                        }

                        storageArrayList.add(storage);
                    }
                    break;
                }
                if (line.equals(""))
                    continue;
                storage.setStorage(tempStr[0]);
                storage.setInvert(Float.parseFloat(tempStr[1]));
                storage.setMaxdepth(Float.parseFloat(tempStr[2]));
                storage.setInitdepth(Float.parseFloat(tempStr[3]));
                storage.setShape(tempStr[4]);

                if ((storage.getShape()).equals("TABULAR"))
                {
                    storage.setCurvename(tempStr[5]);
                    storage.setPonded(Float.parseFloat(tempStr[6]));
                    storage.setFevap(Float.parseFloat(tempStr[7]));
                    if (tempStr.length>8 && !tempStr[8].equals(""))
                    {
                        float[] tempSeepage=new float[3];
                        tempSeepage[0]=(Float.parseFloat(tempStr[8]));
                        tempSeepage[1]=(Float.parseFloat(tempStr[9]));
                        tempSeepage[2]=(Float.parseFloat(tempStr[10]));
                        storage.setSeepage(tempSeepage);
                        storage.setSeepage_exist("YES");
                    }
                    else
                    {
                        storage.setSeepage_exist("NO");
                    }
                }
                else if ((storage.getShape()).equals("FUNCTIONAL"))
                {
                    float[] tempParam=new float[3];
                    tempParam[0]=(Float.parseFloat(tempStr[5]));
                    tempParam[1]=(Float.parseFloat(tempStr[6]));
                    tempParam[2]=(Float.parseFloat(tempStr[7]));
                    storage.setParam(tempParam);
                    storage.setPonded(Float.parseFloat(tempStr[8]));
                    storage.setFevap(Float.parseFloat(tempStr[9]));
                    if (tempStr.length>10 && !tempStr[8].equals(""))
                    {
                        float[] tempSeepage=new float[3];
                        tempSeepage[0]=(Float.parseFloat(tempStr[10]));
                        tempSeepage[1]=(Float.parseFloat(tempStr[11]));
                        tempSeepage[2]=(Float.parseFloat(tempStr[12]));
                        storage.setSeepage(tempSeepage);
                        storage.setSeepage_exist("YES");
                    }
                    else
                    {
                        storage.setSeepage_exist("NO");
                    }
                }

                storageArrayList.add(storage);
            }
            return storageArrayList;

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /*读取[CONDUITS]*/
    @Override
    public ArrayList<InpData.Conduit> readConduit(BufferedReader bufferedReader,BufferedReader bufferedReaderNextline)
    {
        String nextline=null;
        String line= null;
        ArrayList<InpData.Conduit> conduitArrayList=new ArrayList<>();
        try {
            nextline=bufferedReaderNextline.readLine();

            while ((line=bufferedReader.readLine())!=null && (nextline=bufferedReaderNextline.readLine())!=null)
            {
                InpData.Conduit conduit=inpData.new Conduit();
                if (line.startsWith(";;"))
                    continue;
                else if(line.startsWith(";"))
                {
                    ArrayList<String> tempDescription= new ArrayList<>();

                    while (line.startsWith(";"))
                    {
                        tempDescription.add(line.substring(1));
                        line=bufferedReader.readLine();
                        nextline=bufferedReaderNextline.readLine();
                    }
                    conduit.setDescription(tempDescription);

                }
                String[] tempStr=line.split("[ ]+");
                if (nextSection(nextline))
                {
                    if (!line.equals(""))
                    {
                        conduit.setConduit(tempStr[0]);
                        conduit.setFromnode(tempStr[1]);
                        conduit.setTonode(tempStr[2]);
                        conduit.setLength(Float.parseFloat(tempStr[3]));
                        conduit.setRoughness(Float.parseFloat(tempStr[4]));
                        conduit.setInoffset(Float.parseFloat(tempStr[5]));
                        conduit.setOutoffset(Float.parseFloat(tempStr[6]));
                        conduit.setInitflow(Float.parseFloat(tempStr[7]));
                        conduit.setMaxflow(Float.parseFloat(tempStr[8]));
                        conduitArrayList.add(conduit);
                    }
                    break;
                }
                if (line.equals(""))
                    continue;
                conduit.setConduit(tempStr[0]);
                conduit.setFromnode(tempStr[1]);
                conduit.setTonode(tempStr[2]);
                conduit.setLength(Float.parseFloat(tempStr[3]));
                conduit.setRoughness(Float.parseFloat(tempStr[4]));
                conduit.setInoffset(Float.parseFloat(tempStr[5]));
                conduit.setOutoffset(Float.parseFloat(tempStr[6]));
                conduit.setInitflow(Float.parseFloat(tempStr[7]));
                conduit.setMaxflow(Float.parseFloat(tempStr[8]));
                conduitArrayList.add(conduit);
            }
            return conduitArrayList;

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    /*读取[XSECTIONS]*/
    @Override
    public ArrayList<InpData.Xscetion> readXscetion(BufferedReader bufferedReader,BufferedReader bufferedReaderNextline)
    {
        String nextline=null;
        String line= null;
        ArrayList<InpData.Xscetion> xscetionArrayList=new ArrayList<>();
        try {
            nextline=bufferedReaderNextline.readLine();

            while ((line=bufferedReader.readLine())!=null && (nextline=bufferedReaderNextline.readLine())!=null)
            {
                InpData.Xscetion xscetion=inpData.new Xscetion();
                if (line.startsWith(";;"))
                    continue;
                String[] tempStr=line.split("[ ]+");
                if (nextSection(nextline))
                {
                    if (!line.equals(""))
                    {
                        xscetion.setLink(tempStr[0]);
                        xscetion.setShape(tempStr[1]);
                        xscetion.setGeom1(Float.parseFloat(tempStr[2]));
                        xscetion.setGeom2(Float.parseFloat(tempStr[3]));
                        xscetion.setGeom3(Float.parseFloat(tempStr[4]));
                        xscetion.setGeom4(Float.parseFloat(tempStr[5]));
                        if(tempStr.length>6)
                        {
                            xscetion.setBarrels(Integer.parseInt(tempStr[6]));
                        }
                        if (tempStr.length>7 && !tempStr[7].equals(""))
                        {
                            xscetion.setCulvertcode(tempStr[7]);
                        }

                        xscetionArrayList.add(xscetion);
                    }
                    break;
                }
                if (line.equals(""))
                    continue;

                xscetion.setLink(tempStr[0]);
                xscetion.setShape(tempStr[1]);
                xscetion.setGeom1(Float.parseFloat(tempStr[2]));
                xscetion.setGeom2(Float.parseFloat(tempStr[3]));
                xscetion.setGeom3(Float.parseFloat(tempStr[4]));
                xscetion.setGeom4(Float.parseFloat(tempStr[5]));
                if(tempStr.length>6)  //有可能没有barrel，参考swmm示例数据example3
                {
                    xscetion.setBarrels(Integer.parseInt(tempStr[6]));
                }
                if (tempStr.length>7 && !tempStr[7].equals(""))
                {
                    xscetion.setCulvertcode(tempStr[7]);
                }

                xscetionArrayList.add(xscetion);
            }
            return xscetionArrayList;

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /*读取[LOSSES]*/
    @Override
    public ArrayList<InpData.Losses> readLosses(BufferedReader bufferedReader,BufferedReader bufferedReaderNextline)
    {
        String nextline=null;
        String line= null;
        ArrayList<InpData.Losses> lossesArrayList=new ArrayList<>();
        try {
            nextline=bufferedReaderNextline.readLine();

            while ((line=bufferedReader.readLine())!=null && (nextline=bufferedReaderNextline.readLine())!=null)
            {
                InpData.Losses losses=inpData.new Losses();
                if (line.startsWith(";;"))
                    continue;
                String[] tempStr=line.split("[ ]+");
                if (nextSection(nextline))
                {
                    if (!line.equals(""))
                    {
                        losses.setCulvert(tempStr[0]);
                        losses.setEntry_loss_coeff(Float.parseFloat(tempStr[1]));
                        losses.setExit_loss_coeff(Float.parseFloat(tempStr[2]));
                        losses.setAvg_loss_coeff(Float.parseFloat(tempStr[3]));
                        losses.setFlap_gate(tempStr[4]);
                        losses.setSeepage_loss_rate(Float.parseFloat(tempStr[5]));
                        lossesArrayList.add(losses);
                    }
                    break;
                }
                if (line.equals(""))
                    continue;
                losses.setCulvert(tempStr[0]);
                losses.setEntry_loss_coeff(Float.parseFloat(tempStr[1]));
                losses.setExit_loss_coeff(Float.parseFloat(tempStr[2]));
                losses.setAvg_loss_coeff(Float.parseFloat(tempStr[3]));
                losses.setFlap_gate(tempStr[4]);
                losses.setSeepage_loss_rate(Float.parseFloat(tempStr[5]));
                lossesArrayList.add(losses);
            }
            return lossesArrayList;

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /*读取[CURVES]*/
    @Override
    public ArrayList<InpData.Curve> readCurve(BufferedReader bufferedReader,BufferedReader bufferedReaderNextline)
    {
        int n=0;
        String nextline=null;
        String line= null;
        ArrayList<InpData.Curve> curveArrayList=new ArrayList<>();
        try {
            nextline=bufferedReaderNextline.readLine();

            while ((line=bufferedReader.readLine())!=null && (nextline=bufferedReaderNextline.readLine())!=null)
            {
                InpData.Curve curve=inpData.new Curve();
                ArrayList<InpData.CurveXY> curveXYArrayList=new ArrayList<>();
                ArrayList<String> tempDescription =new ArrayList<>();

                if (line.startsWith(";;"))
                    continue;

                if (nextSection(nextline))
                {
                    if (line.equals("") || line.equals(";"))
                        continue;
                    if (line.startsWith(";") && line.substring(1)!=null)
                    {
                        tempDescription.add(line.substring(1));
                        line=bufferedReader.readLine();
                        nextline=bufferedReaderNextline.readLine();
                    }
                    if (tempDescription.size()>0)
                        curve.setDescription(tempDescription);
                    if (!line.startsWith(";") && !line.equals(""))
                    {

                        String[] tempStr=line.split("[ ]+");
                        if (n==0)
                        {
                            curve.setCurve(tempStr[0]);
                            curve.setType(tempStr[1]);
                            InpData.CurveXY curveXY=inpData.new CurveXY();
                            curveXY.setX_value(Float.parseFloat(tempStr[2]));
                            curveXY.setY_value(Float.parseFloat(tempStr[3]));
                            curveXYArrayList.add(curveXY);
                            n++;
                            line=bufferedReader.readLine();
                            nextline=bufferedReaderNextline.readLine();
                        }
                        else
                        {
                            InpData.CurveXY curveXY=inpData.new CurveXY();
                            curveXY.setX_value(Float.parseFloat(tempStr[1]));
                            curveXY.setY_value(Float.parseFloat(tempStr[2]));
                            curveXYArrayList.add(curveXY);
                        }
                    }
                    if (curveXYArrayList.size()!=0)
                        curve.setXyArrayList(curveXYArrayList);
                    n=0;
                    curveArrayList.add(curve);
                    break;
                }
                if (line.equals("") || line.equals(";"))
                    continue;
                while (line.startsWith(";") && line.substring(1)!=null)
                {
                    tempDescription.add(line.substring(1));
                    line=bufferedReader.readLine();
                    nextline=bufferedReaderNextline.readLine();
                }
                if (tempDescription.size()>0)
                    curve.setDescription(tempDescription);
                while (!line.startsWith(";") && !line.equals(""))
                {
                    if (nextSection(nextline))
                        break;
                    String[] tempStr=line.split("[ ]+");
                    if (n==0)
                    {
                        curve.setCurve(tempStr[0]);
                        curve.setType(tempStr[1]);
                        InpData.CurveXY curveXY=inpData.new CurveXY();
                        curveXY.setX_value(Float.parseFloat(tempStr[2]));
                        curveXY.setY_value(Float.parseFloat(tempStr[3]));
                        curveXYArrayList.add(curveXY);
                        n++;
                        line=bufferedReader.readLine();
                        nextline=bufferedReaderNextline.readLine();
                    }
                    else
                    {
                        InpData.CurveXY curveXY=inpData.new CurveXY();
                        curveXY.setX_value(Float.parseFloat(tempStr[1]));
                        curveXY.setY_value(Float.parseFloat(tempStr[2]));
                        curveXYArrayList.add(curveXY);
                        line=bufferedReader.readLine();
                        nextline=bufferedReaderNextline.readLine();
                    }
                }
                if (curveXYArrayList.size()!=0)
                    curve.setXyArrayList(curveXYArrayList);
                n=0;
                curveArrayList.add(curve);
                if (nextSection(nextline))
                {
                    break;
                }

            }
            return curveArrayList;

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /*读取[TIMESERIES]*/
    @Override
    public ArrayList<InpData.Timeseries> readTimeseries(BufferedReader bufferedReader,BufferedReader bufferedReaderNextline)
    {
        int n=0;
        String nextline=null;
        String line= null;
        ArrayList<InpData.Timeseries> timeseriesArrayList=new ArrayList<>();
        try {
            nextline=bufferedReaderNextline.readLine();
            while ((line=bufferedReader.readLine())!=null && (nextline=bufferedReaderNextline.readLine())!=null)
            {

                ArrayList<String> tempDescription =new ArrayList<>();
                InpData.Timeseries timeseries=inpData.new Timeseries();
                if (line.startsWith(";;"))
                    continue;
                if (line.equals(";"))
                {
                    line=bufferedReader.readLine();
                    nextline=bufferedReaderNextline.readLine();
                }

                while (line.startsWith(";") && line.substring(1) != null)
                {
                    tempDescription.add(line.substring(1));
                    line = bufferedReader.readLine();
                    nextline = bufferedReaderNextline.readLine();
                }
                if (tempDescription.size()>0)
                    timeseries.setDescription(tempDescription);


                ArrayList<InpData.DateTimeValue> dateTimeValueArrayList = new ArrayList<>();
                while (!line.startsWith(";") && line != null) {
                    String[] tempStr = line.split("[ ]+");
                    if (n == 0) {
                        timeseries.setTimeseries(tempStr[0]);
                        InpData.DateTimeValue dateTimeValue = inpData.new DateTimeValue();
                        if (tempStr.length == 4) {
                            dateTimeValue.setDate(tempStr[1]);
                            dateTimeValue.setTime(tempStr[2]);
                            dateTimeValue.setValue(Float.parseFloat(tempStr[3]));
                        } else if (tempStr.length == 3) {
                            dateTimeValue.setTime(tempStr[1]);
                            dateTimeValue.setValue(Float.parseFloat(tempStr[2]));
                        }
                        dateTimeValueArrayList.add(dateTimeValue);
                        n++;
                        line = bufferedReader.readLine();
                        nextline = bufferedReaderNextline.readLine();
                        if (nextSection(nextline))
                            break;
                    } else {
                        InpData.DateTimeValue dateTimeValue = inpData.new DateTimeValue();
                        if (tempStr.length == 4) {
                            dateTimeValue.setDate(tempStr[1]);
                            dateTimeValue.setTime(tempStr[2]);
                            dateTimeValue.setValue(Float.parseFloat(tempStr[3]));
                        } else if (tempStr.length == 3) {
                            dateTimeValue.setTime(tempStr[1]);
                            dateTimeValue.setValue(Float.parseFloat(tempStr[2]));
                        }
                        dateTimeValueArrayList.add(dateTimeValue);
                        n++;
                        line = bufferedReader.readLine();
                        nextline = bufferedReaderNextline.readLine();
                        if (nextSection(nextline))
                            break;
                    }

                }
                n = 0;
                timeseries.setDateTimeValueArrayList(dateTimeValueArrayList);
                timeseriesArrayList.add(timeseries);
                if (nextSection(nextline))
                    break;
            }
            return timeseriesArrayList;

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /*读取[REPORT]*/
    @Override
    public InpData.Report readReport(BufferedReader bufferedReader,BufferedReader bufferedReaderNextline)
    {
        String nextline=null;
        String line= null;
        try {
            nextline=bufferedReaderNextline.readLine();
            InpData.Report report=inpData.new Report();
            while ((line=bufferedReader.readLine())!=null && (nextline=bufferedReaderNextline.readLine())!=null)
            {

                if (line.startsWith(";;"))
                    continue;
                String[] tempStr=line.split("[ ]+");
                if (nextSection(nextline))
                {
                    if (!line.equals(""))
                    {
                        if (tempStr[0].equals("INPUT"))
                            report.setInput(tempStr[1]);
                        else if (tempStr[0].equals("CONTROLS"))
                            report.setControls(tempStr[1]);
                        else if (tempStr[0].equals("SUBCATCHMENTS"))
                        {
                            String[] tempSubcatchments=new String[tempStr.length-1];
                            for (int i=0;i<tempSubcatchments.length;i++)
                            {
                                tempSubcatchments[i]=tempStr[i+1];
                            }
                            report.setSubcatchments(tempSubcatchments);
                        }
                        else if (tempStr[0].equals("NODES"))
                        {
                            String[] tempNodes=new String[tempStr.length-1];
                            for (int i=0;i<tempNodes.length;i++)
                            {
                                tempNodes[i]=tempStr[i+1];
                            }
                            report.setNodes(tempNodes);
                        }
                        else if (tempStr[0].equals("LINKS"))
                        {
                            String[] tempLinks=new String[tempStr.length-1];
                            for (int i=0;i<tempLinks.length;i++)
                            {
                                tempLinks[i]=tempStr[i+1];
                            }
                            report.setLinks(tempLinks);
                        }
                        else
                        {

                        }
                    }
                    break;
                }
                if (line.equals(""))
                    continue;
                if (!line.equals(""))
                {
                    if (tempStr[0].equals("INPUT"))
                        report.setInput(tempStr[1]);
                    else if (tempStr[0].equals("CONTROLS"))
                        report.setControls(tempStr[1]);
                    else if (tempStr[0].equals("SUBCATCHMENTS"))
                    {
                        String[] tempSubcatchments=new String[tempStr.length-1];
                        for (int i=0;i<tempSubcatchments.length;i++)
                        {
                            tempSubcatchments[i]=tempStr[i+1];
                        }
                        report.setSubcatchments(tempSubcatchments);
                    }
                    else if (tempStr[0].equals("NODES"))
                    {
                        String[] tempNodes=new String[tempStr.length-1];
                        for (int i=0;i<tempNodes.length;i++)
                        {
                            tempNodes[i]=tempStr[i+1];
                        }
                        report.setNodes(tempNodes);
                    }
                    else if (tempStr[0].equals("LINKS"))
                    {
                        String[] tempLinks=new String[tempStr.length-1];
                        for (int i=0;i<tempLinks.length;i++)
                        {
                            tempLinks[i]=tempStr[i+1];
                        }
                        report.setLinks(tempLinks);
                    }
                    else
                    {

                    }
                }

            }
            return report;

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    /*读取[TAGS]*/
    @Override
    public ArrayList<InpData.Tag> readTag(BufferedReader bufferedReader,BufferedReader bufferedReaderNextline)
    {
        String nextline=null;
        String line= null;
        ArrayList<InpData.Tag> tagArrayList=new ArrayList<>();
        try {
            nextline=bufferedReaderNextline.readLine();
            while ((line=bufferedReader.readLine())!=null && (nextline=bufferedReaderNextline.readLine())!=null)
            {
                InpData.Tag tag=inpData.new Tag();
                if (line.startsWith(";;"))
                    continue;
                String[] tempStr=line.split("[ ]+");
                if (nextSection(nextline))
                {
                    if (!line.equals(""))
                    {
                        tag.setCategory(tempStr[0]);
                        tag.setTagId(tempStr[1]);
                        tag.setContent(tempStr[2]);
                        tagArrayList.add(tag);
                    }
                    break;
                }
                if (line.equals(""))
                    continue;
                tag.setCategory(tempStr[0]);
                tag.setTagId(tempStr[1]);
                tag.setContent(tempStr[2]);
                tagArrayList.add(tag);
            }
            return tagArrayList;

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    /*读取[MAP]*/
    @Override
    public InpData.Map readMap(BufferedReader bufferedReader,BufferedReader bufferedReaderNextline)
    {
        String line=null;
        String nextline=null;
        InpData.Map map=inpData.new Map();
        try
        {
            nextline=bufferedReaderNextline.readLine();
            while ((line=bufferedReader.readLine())!=null &&  (nextline=bufferedReaderNextline.readLine())!=null)
            {
                String[] tempStr=line.split("[ ]+");
                if (line.startsWith(";;"))
                    continue;
                if (nextSection(nextline))
                {
                    if (!line.equals(""))
                    {
                        if (tempStr[0].equals("DIMENSIONS"))
                        {
                            float[] tempDimensions=new float[tempStr.length-1];
                            for (int i=0;i<tempDimensions.length;i++)
                            {
                                tempDimensions[i]=Float.parseFloat(tempStr[i+1]);
                            }
                            map.setDimensions(tempDimensions);
                        }
                        else if (tempStr[0].equals("Units"))
                        {
                            map.setUnits(tempStr[1]);
                        }
                    }
                    break;
                }
                if (line.equals(""))
                    continue;

                if (tempStr[0].equals("DIMENSIONS"))
                {
                    float[] tempDimensions=new float[tempStr.length-1];
                    for (int i=0;i<tempDimensions.length;i++)
                    {
                        tempDimensions[i]=Float.parseFloat(tempStr[i+1]);
                    }
                    map.setDimensions(tempDimensions);
                }
                else if (tempStr[0].equals("Units"))
                {
                    map.setUnits(tempStr[1]);
                }
            }
            return map;

        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }


    /*读取[COORDINATES]*/
    @Override
    public ArrayList<InpData.Coordinate> readCoordinate(BufferedReader bufferedReader,BufferedReader bufferedReaderNextline)
    {
        String nextline=null;
        String line= null;
        ArrayList<InpData.Coordinate> coordinateArrayList=new ArrayList<>();
        try {
            nextline=bufferedReaderNextline.readLine();

            while ((line=bufferedReader.readLine())!=null && (nextline=bufferedReaderNextline.readLine())!=null)
            {
                InpData.Coordinate coordinate=inpData.new Coordinate();
                if (line.startsWith(";;"))
                    continue;
                String[] tempStr=line.split("[ ]+");
                if (nextSection(nextline))
                {
                    if (!line.equals(""))
                    {
                        coordinate.setNode(tempStr[0]);
                        coordinate.setX_coord(Float.parseFloat(tempStr[1]));
                        coordinate.setY_coord(Float.parseFloat(tempStr[2]));
                        coordinateArrayList.add(coordinate);
                    }
                    break;
                }
                if (line.equals(""))
                    continue;
                coordinate.setNode(tempStr[0]);
                coordinate.setX_coord(Float.parseFloat(tempStr[1]));
                coordinate.setY_coord(Float.parseFloat(tempStr[2]));
                coordinateArrayList.add(coordinate);
            }
            return coordinateArrayList;

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /*读取[VERTICES]*/
    @Override
    public ArrayList<InpData.Vertice> readVertice(BufferedReader bufferedReader,BufferedReader bufferedReaderNextline)
    {
        String nextline=null;
        String line= null;
        ArrayList<InpData.Vertice> verticeArrayList=new ArrayList<>();
        try {
            nextline=bufferedReaderNextline.readLine();

            while ((line=bufferedReader.readLine())!=null && (nextline=bufferedReaderNextline.readLine())!=null)
            {
                InpData.Vertice vertice=inpData.new Vertice();
                if (line.startsWith(";;"))
                    continue;
                String[] tempStr=line.split("[ ]+");
                if (nextSection(nextline))
                {
                    if (!line.equals(""))
                    {
                        vertice.setLink(tempStr[0]);
                        vertice.setX_coord(Float.parseFloat(tempStr[1]));
                        vertice.setY_coord(Float.parseFloat(tempStr[2]));
                        verticeArrayList.add(vertice);
                    }
                    break;
                }
                if (line.equals(""))
                    continue;
                vertice.setLink(tempStr[0]);
                vertice.setX_coord(Float.parseFloat(tempStr[1]));
                vertice.setY_coord(Float.parseFloat(tempStr[2]));
                verticeArrayList.add(vertice);
            }
            return verticeArrayList;

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /*读取[Polygons]*/
    @Override
    public ArrayList<InpData.Polygon> readPolygons(BufferedReader bufferedReader,BufferedReader bufferedReaderNextline)
    {
        String nextline=null;
        String line= null;
        ArrayList<InpData.Polygon> polygonsArrayList=new ArrayList<>();
        try {
            nextline=bufferedReaderNextline.readLine();

            while ((line=bufferedReader.readLine())!=null && (nextline=bufferedReaderNextline.readLine())!=null)
            {
                InpData.Polygon polygon=inpData.new Polygon();
                if (line.startsWith(";;"))
                    continue;
                String[] tempStr=line.split("[ ]+");
                if (nextSection(nextline))
                {
                    if (!line.equals(""))
                    {
                        polygon.setPolygon(tempStr[0]);
                        polygon.setX_coord(Float.parseFloat(tempStr[1]));
                        polygon.setY_coord(Float.parseFloat(tempStr[2]));
                        polygonsArrayList.add(polygon);
                    }
                    break;
                }
                if (line.equals(""))
                    continue;
                polygon.setPolygon(tempStr[0]);
                polygon.setX_coord(Float.parseFloat(tempStr[1]));
                polygon.setY_coord(Float.parseFloat(tempStr[2]));
                polygonsArrayList.add(polygon);
            }
            return polygonsArrayList;

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    /*读取[SYMBOLS]*/
    @Override
    public ArrayList<InpData.Symbol> readSymbol(BufferedReader bufferedReader,BufferedReader bufferedReaderNextline)
    {
        String nextline=null;
        String line= null;
        ArrayList<InpData.Symbol> symbolArrayList=new ArrayList<>();
        try {
            nextline=bufferedReaderNextline.readLine();

            while ((line=bufferedReader.readLine())!=null && (nextline=bufferedReaderNextline.readLine())!=null)
            {
                InpData.Symbol symbol=inpData.new Symbol();
                if (line.startsWith(";;"))
                    continue;
                String[] tempStr=line.split("[ ]+");
                if (nextSection(nextline))
                {
                    if (!line.equals(""))
                    {
                        symbol.setGage(tempStr[0]);
                        symbol.setX_coord(Float.parseFloat(tempStr[1]));
                        symbol.setY_coord(Float.parseFloat(tempStr[2]));
                        symbolArrayList.add(symbol);
                    }
                    break;
                }
                if (line.equals(""))
                    continue;
                symbol.setGage(tempStr[0]);
                symbol.setX_coord(Float.parseFloat(tempStr[1]));
                symbol.setY_coord(Float.parseFloat(tempStr[2]));
                symbolArrayList.add(symbol);
            }
            return symbolArrayList;

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    /*判断游标是否到了下一节*/
    @Override
    public boolean nextSection(String line)
    {
        for (InpSection section : InpSection.values()) {
            if (line.equals("["+section+"]"))
            {
                return true;
            }

        }
        return false;
    }

    private ArrayList<String> removeEmptyFromArray(ArrayList<String>  originArrayList)
    {

        ArrayList<String> trimedArrayList= new ArrayList<>();
        for (int i=0;i<originArrayList.size();i++)
        {
            if(originArrayList.get(i)!=null && !originArrayList.get(i).equals(""))
                trimedArrayList.add(originArrayList.get(i));
        }

        return trimedArrayList;
    }
    private ArrayList<String> removeEmptyFromArray(String[]  originArray)
    {

        List<String> originArrayList=Arrays.asList(originArray);
        ArrayList<String> trimedArrayList= new ArrayList<>();
        for (int i=0;i<originArrayList.size();i++)
        {
            if(originArrayList.get(i)!=null && !originArrayList.get(i).equals(""))
                trimedArrayList.add(originArrayList.get(i));
        }

        return trimedArrayList;
    }



    /*将coordinates转成geojson*/
    public JSONObject getGeojsonFromInp()
    {
        List<InpData.Coordinate> coordinateList=inpData.getCoordinateArrayList();
        JSONObject jo=new JSONObject();
        jo.put("type","FeatureCollection");

        List<JSONObject> jsonObjects = new ArrayList<>();
        if(coordinateList!=null) {
            for (int i = 0; i < coordinateList.size(); i++) {
                JSONObject jsonPointObject = new JSONObject();
                jsonPointObject.put("type", "Feature");

                JSONObject jsonPointObject_1 = new JSONObject();
                jsonPointObject_1.put("type", "Point");

                float[] coordinate = new float[]{coordinateList.get(i).getX_coord(), coordinateList.get(i).getY_coord()};
                jsonPointObject_1.put("coordinates", coordinate);
                jsonPointObject.put("geometry", jsonPointObject_1);

                JSONObject jsonPointObject_2 = new JSONObject();
                jsonPointObject_2.put("name", coordinateList.get(i).getNode());
                jsonPointObject.put("properties", jsonPointObject_2);
                jsonObjects.add(jsonPointObject);
            }
        }

        List<InpData.Conduit> conduitList=inpData.getConduitArrayList();
        List<JSONObject> jsonPolylineObjects=new ArrayList<>();
        if(conduitList!=null) {
            for (int i = 0; i < conduitList.size(); i++) {
                JSONObject jsonPolylineObject = new JSONObject();
                jsonPolylineObject.put("type", "Feature");

                JSONObject jsonPolylineObject_1 = new JSONObject();
                jsonPolylineObject_1.put("type", "LineString");

                Polyline polyline = getPolylineCoordinates(conduitList.get(i));
                float[][] coordinates = {{polyline.getFrom_x(), polyline.getFrom_y()}, {polyline.getTo_x(), polyline.getTo_y()}};
                jsonPolylineObject_1.put("coordinates", coordinates);
                jsonPolylineObject.put("geometry", jsonPolylineObject_1);

                JSONObject jsonPolylineObject_2 = new JSONObject();
                jsonPolylineObject_2.put("name", conduitList.get(i).getConduit());
                jsonPolylineObject.put("properties", jsonPolylineObject_2);
                jsonObjects.add(jsonPolylineObject);
            }
        }

        List<Polygon> polygonList=getPolygonCoordinates();
        //生成polygon的geojson
        if(polygonList!=null) {
            for (int i = 0; i < polygonList.size(); i++) {
                JSONObject jsonPolygonObject = new JSONObject();
                jsonPolygonObject.put("type", "Feature");

                JSONObject jsonPolygonObject_1 = new JSONObject();
                jsonPolygonObject_1.put("type", "Polygon");

                Polygon polygon = polygonList.get(i);
                float[][][] coordinates = new float[1][polygon.getCoordinateList().size()][2];
                for (int j = 0; j < polygon.getCoordinateList().size(); j++) {
                    coordinates[0][j] = new float[2];
                    coordinates[0][j][0] = polygon.getCoordinateList().get(j).getX();
                    coordinates[0][j][1] = polygon.getCoordinateList().get(j).getY();
                }

                jsonPolygonObject_1.put("coordinates", coordinates);
                jsonPolygonObject.put("geometry", jsonPolygonObject_1);

                JSONObject jsonPolygonObject_2 = new JSONObject();
                jsonPolygonObject_2.put("name", polygonList.get(i).getName());
                jsonPolygonObject.put("properties", jsonPolygonObject_2);
                jsonObjects.add(jsonPolygonObject);
            }
        }
        jo.put("features",jsonObjects);
        //TODO
        return jo;
    }

    private class Polyline{
        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public float getFrom_x() {
            return from_x;
        }

        public void setFrom_x(float from_x) {
            this.from_x = from_x;
        }

        public float getFrom_y() {
            return from_y;
        }

        public void setFrom_y(float from_y) {
            this.from_y = from_y;
        }

        public float getTo_x() {
            return to_x;
        }

        public void setTo_x(float to_x) {
            this.to_x = to_x;
        }

        public float getTo_y() {
            return to_y;
        }

        public void setTo_y(float to_y) {
            this.to_y = to_y;
        }

        private String name;
        private float from_x;
        private float from_y;
        private float to_x;
        private float to_y;
    }

    private Polyline getPolylineCoordinates(InpData.Conduit line)
    {
        Polyline polyline=new Polyline();
        String from_node=line.getFromnode();
        String to_node=line.getTonode();
        polyline.setName(line.getConduit());
        for (int i=0;i<inpData.getCoordinateArrayList().size();i++)
        {
            InpData.Coordinate point=inpData.getCoordinateArrayList().get(i);
            if (from_node.equals(point.getNode()))
            {
                polyline.setFrom_x(point.getX_coord());
                polyline.setFrom_y(point.getY_coord());
            }
            else if (to_node.equals(point.getNode()))
            {
                polyline.setTo_x(point.getX_coord());
                polyline.setTo_y(point.getY_coord());
            }
            else {}
        }
        return polyline;
    }

    private class Point{
        public float getX() {
            return x;
        }

        public void setX(float x) {
            this.x = x;
        }

        public float getY() {
            return y;
        }

        public void setY(float y) {
            this.y = y;
        }

        private float x;
        private float y;
    }

    private class Polygon{
        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }



        private String name;

        public List<Point> getCoordinateList() {
            return coordinateList;
        }

        public void setCoordinateList(List<Point> coordinateList) {
            this.coordinateList = coordinateList;
        }

        private List<Point> coordinateList;

    }

    private List<Polygon> getPolygonCoordinates()
    {
        List<InpData.Polygon> polygonList=inpData.getPolygonArrayList();
        List<Polygon> polygons=new ArrayList<>();
        List<JSONObject> jsonPolygonObjects=new ArrayList<>();
        //求出polygon的个数及每个polygon对应的点
        for (int i=0;i<polygonList.size();i++)
        {
            Polygon polygon = new Polygon();
            polygon.setName(polygonList.get(i).getPolygon());
            List<Point> pointList=new ArrayList<>();
            while (i<polygonList.size()-1 && polygonList.get(i).getPolygon().equals(polygonList.get(i + 1).getPolygon())) {
                Point point=new Point();
                point.setX(polygonList.get(i).getX_coord());
                point.setY(polygonList.get(i).getY_coord());
                pointList.add(point);
                i++;
            }
            Point point=new Point();
            point.setX(polygonList.get(i).getX_coord());
            point.setY(polygonList.get(i).getY_coord());
            pointList.add(point);
            polygon.setCoordinateList(pointList);
            polygons.add(polygon);

        }
        return polygons;
    }


    /*写geojson文件*/

}
